// 窗口检测，并执行添加html节点的tag
(function() {
  interface deviceProps {
    os?: string;
    android?: boolean;
    ios?: boolean;
    osVersion?: any;
    iphone?: boolean;
    ipad?: boolean;
    pixelRatio?: number;
    webView?: any;
    androidChrome?: boolean;
  }

  let device: deviceProps = {};

  const ua = navigator.userAgent;
  const android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
  const ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
  const ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
  const iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);

  // Android
  if (android) {
    device.os = 'android';
    device.osVersion = android[2];
    device.android = true;
    device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0;
  }
  if (ipad || iphone || ipod) {
    device.os = 'ios';
    device.ios = true;
  }
  // iOS
  if (iphone && !ipod) {
    device.osVersion = iphone[2].replace(/_/g, '.');
    device.iphone = true;
  }
  if (ipad) {
    device.osVersion = ipad[2].replace(/_/g, '.');
    device.ipad = true;
  }
  if (ipod) {
    device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : '';
    device.iphone = true;
  }
  // iOS 8+ changed UA
  if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
    if (device.osVersion.split('.')[0] === '10') {
      device.osVersion = ua
        .toLowerCase()
        .split('version/')[1]
        .split(' ')[0];
    }
  }
  // Webview
  device.webView =
    (iphone || ipad || ipod) && ua.match(/.*AppleWebKit(?!.*Safari)/i);
  // Classes
  let classNames = [];

  // Pixel Ratio
  device.pixelRatio = window.devicePixelRatio || 1;
  classNames.push(`pixel-ratio-${Math.floor(device.pixelRatio)}`);

  // Add html classes
  if (classNames.length > 0) {
    const html = document.getElementsByTagName('html')[0];
    // html.setAttribute('class', classNames.join(' '))//加class的话编译需要hash值
    html.setAttribute('tag', classNames.join(' '));
  }
})();
